package com.igoal.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.igoal.dto.GoalDTO;
import com.igoal.dto.UserDTO;
import com.igoal.dto.UserGoalEntryDoneDTO;
import com.igoal.service.GoalEntryManager;
import com.igoal.service.GoalManager;
import com.igoal.service.MessageManager;
import com.igoal.service.SaveResult;
import com.igoal.service.UserManagerExt;
import com.igoal.utils.ConvertUtils;
import com.igoal.web.model.BaseModel;

@Controller
@RequestMapping("/done/editEntry.*")
public class HowIDidItController extends BaseFormController {
	@Autowired
	public void setUserManagerExt(UserManagerExt userManagerExt) {
		this.userManagerExt = userManagerExt;
	}

	@Autowired
	public void setGoalManager(GoalManager goalManager) {
		this.goalManager = goalManager;
	}

	@Autowired
	public void setMessageManager(MessageManager messageManager) {
		this.messageManager = messageManager;
	}

	@Autowired
	public void setGoalEntryManager(GoalEntryManager goalEntryManager) {
		this.goalEntryManager = goalEntryManager;
	}
	
	@Override
	public String getViewPrefix() {
		return BaseControllerDelegate.DEFAULT_VIEW_PREFIX;
	}

	public HowIDidItController() {
		setCommandName("entry");
		setCommandClass(UserGoalEntryDoneDTO.class);
		setFormView(getViewPrefix() + "howidone");
		setSuccessView("redirect:/dashboard.html");
	}

	@Override
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		UserGoalEntryDoneDTO entry = null;
		String goalId = request.getParameter("goalId");
		String entryId = request.getParameter("entryId");
		UserDTO user = getUserSession();

		if (goalId != null) {
			try {
				entry = goalEntryManager.getGoalDoneEntry(ConvertUtils.toLong(goalId, -1L),
						user.getId());
			} catch (Exception e) {
				logger.error(e);
			}

			if (entry == null) {
				entry = new UserGoalEntryDoneDTO();
			}

			Long id = ConvertUtils.toLong(goalId, -1L);
			GoalDTO goalDTO = goalManager.getGoalByPrimaryKey(id);

			if (goalDTO == null) {
				logger.error("Request page how i did it with non found goal id: " + goalId);
				return new UserGoalEntryDoneDTO();
			}

			entry.setGoalId(id);
			entry.setGoalTitle(goalDTO.getTitle());
		} else if (entryId != null) {
			Long id = ConvertUtils.toLong(entryId, -1L);
			return goalEntryManager.getEntryDone(id, user);
		} else {
			return new UserGoalEntryDoneDTO();
		}

		return entry;
	}

	@Override
	protected ModelAndView onSubmit(Object command, BindException errors)
			throws Exception {
		UserGoalEntryDoneDTO entry = (UserGoalEntryDoneDTO) command;
		UserDTO user = getUserSession();

		int result = SaveResult.FAIL;

		if (goalEntryManager.getGoalDoneEntry(entry.getGoalId(), user.getId()) != null) {
			result = goalEntryManager.updateGoalDone(entry);
		} else {
			result = goalEntryManager.createNewGoalDone(entry, user);
		}

		if (result != SaveResult.SUCCESS) {
			logger.error("Cannot save new how i did it entry");
			return new ModelAndView(getSuccessView());
		} else {
			return new ModelAndView(getSuccessView());
		}
	}

	@SuppressWarnings("rawtypes")
	@Override
	protected Map referenceData(HttpServletRequest request) throws Exception {
		ModelMap modelMap = new ModelMap();
		BaseModel model = new BaseModel();
//		UserDTO user = getUserSession();
//		simpleModelInitialize(user, model);
//		initPeopleYouMayKnow(user, model);
		initTopPage(model, request);
		initRightPage(model, request);
		initLeftPage(model, request);
		applyModel(modelMap, model);

		return modelMap;
	}

	public void initTopPage(BaseModel model, HttpServletRequest request) {
		UserDTO user = getUserSession();
		initTopZone(user, model);
	}

	public void initRightPage(BaseModel model, HttpServletRequest request) {
		UserDTO user = getUserSession();
		initRightZone(user, model);
		initPeopleYouMayKnow(user, model);
	}

	public void initLeftPage(BaseModel model, HttpServletRequest request) {}
}
